package com.common.http.basecore.bean.response;

/**
 * Created by lajin on 16/5/17.
 */
public class HttpBaseResponseUtils {
    public static interface HttpResponseCode {
        int HTTP_SUCCESS = 1;                // 1 正常
        int HTTP_INVALIDATE_PARAMETERS = 2;  // 2 参数异常	参数错误
        int HTTP_SERVER_ERROR = 3;           // 3 系统异常	后台处理异常，不显示给用户
        int HTTP_BUSINESS_ERROR = 4;         // 4 业务异常	业务逻辑异常，可直接显示给用户
        int HTTP_TOKEN_ERROR = 5;            // 5 token异常	超时，强制登出重新生成
        int HTTP_SPECIAL_SERVER_ERROR = 6;   // 6 其他需要特殊处理的业务异常
        int HTTP_UNKNOWN_EXCEPTION = 0;   // 0 异常
    }
    /**
     * Http响应码处理
     * @param obj 响应结果, 会处理为BaseResponse子类的响应结果
     * @return 是否处理过这个响应, 当响应码异常时, 返回true, 否则返回false
     */
    public static boolean responseHandle(Object obj) {
        if(obj instanceof AbsBaseReponse) {
            AbsBaseReponse response = (AbsBaseReponse) obj;
            int status = response.getHeader().getStatus();
            switch (status) {
                case HttpResponseCode.HTTP_SUCCESS: // 1 正常
                    return false;
                case HttpResponseCode.HTTP_SERVER_ERROR: // 3 系统异常	后台处理异常，不显示给用户
                    throw new NoneTipException("Server System Error!");
                case HttpResponseCode.HTTP_TOKEN_ERROR: // 5 token异常	超时，强制登出重新生成
                    String exceptionMsg = response.getHeader().getMessage();
                    throw new TokenInvalidException(exceptionMsg);
                case HttpResponseCode.HTTP_UNKNOWN_EXCEPTION:// 0 异常
                case HttpResponseCode.HTTP_INVALIDATE_PARAMETERS: // 2 参数异常	参数错误
                case HttpResponseCode.HTTP_BUSINESS_ERROR: // 4 业务异常	业务逻辑异常，可直接显示给用户
                case HttpResponseCode.HTTP_SPECIAL_SERVER_ERROR: // 6 其他需要特殊处理的业务异常
                    throw new TipException(response.getHeader().getMessage(), status);
            }
        } else {
            return false;
        }
        return false;
    }

    /**
     * Token非法异常, 需要用户重新登录
     */
    public static class TokenInvalidException extends RuntimeException {

        public TokenInvalidException(String exceptionMsg) {
            super(exceptionMsg);
        }
    }


    /**
     * 需要提示用户的异常
     */
    public static class TipException extends RuntimeException {

        private int code;

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public TipException(String message) {
            super(message);
        }

        public TipException(String message, int code) {
            super(message);
            this.code = code;
        }

    }

    /**
     * 已经提示用户的异常
     */
    public static class NoneTipException extends  RuntimeException {

        public NoneTipException(String message) {
            super(message);
        }

    }
    /**
     * 网络异常
     */
    public static class NetWorkTipException extends  RuntimeException {
        public NetWorkTipException(String message) {
            super(message);
        }

    }
}
